import tkinter
import tkmacosx
import pymysql
import weathe_api as weather
from PIL import Image, ImageTk, ImageSequence
import gui_show_one
import gui_liuyan
def resize(w_box, h_box, pil_image):
    # 参数是：要适应的窗口宽、高、Image.open后的图片
    # 处理自适应大小
    w, h = pil_image.size  # 获取图像的原始大小
    f1 = 1.0 * w_box / w
    f2 = 1.0 * h_box / h
    factor = min([f1, f2])
    width = int(w * factor)
    height = int(h * factor)
    return pil_image.resize((width, height), Image.ANTIALIAS)


def check(name, en_name):
    gui_show_one.create_one(name, en_name)


def create_index(usr):
    root = tkinter.Tk()
    root.geometry("900x600")
    root.title(str(usr)+" ! 欢迎你")
    frame = tkmacosx.SFrame(root, width=900, height=600)
    frame.grid(row=0, column=0)
    f1 = tkmacosx.SFrame(frame, width=900, height=50)
    f1.grid(row=0, column=0)
    index_label = tkinter.Label(f1, text="算法可视化平台", fg='black', font=("微软雅黑", 30, 'bold'), width=25)
    index_label.grid(row=0, column=5, columnspan=2)
    f2 = tkmacosx.SFrame(frame, width=900, height=150)
    f2.grid(row=2, column=0)
    ls = ["冒泡排序法", "选择排序法", "猴子排序法", "快速排序法", "梳子排序法", "桶排序", "归并排序法", "圈排序", "堆排序", "插入排序", "基数排序法"]
    ls2 =["BubbleSort", "SelectionSort", "MonkeySort", "QuickSort", "CombSort", "BucketSort", "MergeSort", "CycleSort","HeapSort","InsertionSort","RadixSort"]
    for i, name in enumerate(ls):
        img1 = Image.open(r'../non/3.png')
        img1 = resize(10, 10, img1)
        img2 = ImageTk.PhotoImage(img1)
        img_label = tkinter.Label(f2, text=ls2[i],font=('', 20))
        if i < 6:
            button = tkmacosx.Button(f2, text=name, bg="#339933", fg="white", width=180, height=40,
                                     command=lambda name=name,x=ls2[i]: check(name,x))
            img_label.grid(row=1, column=i)
            button.grid(row=2, column=i)
        else:
            button = tkmacosx.Button(f2, text=name, bg="#339933", fg="white", width=180, height=40,
                                     command=lambda name=name,x=ls2[i]: check(name,x))
            img_label.grid(row=3, column=i - 6)
            button.grid(row=4, column=i - 6)

    # 新增介绍UI
    f3 = tkmacosx.SFrame(frame, width=900, height=300)
    f3.grid(row=1, column=0)
    img3 = Image.open(r'../img/index.jpg')
    img3 = resize(450, 450, img3)
    img3 = ImageTk.PhotoImage(img3)
    img_label2 = tkinter.Label(f3, image=img3)
    img_label2.grid(row=0, column=0,rowspan=4)
    area = tkinter.Text(f3, height=10, width=55)
    str1 = """    本平台是基于数据结构经典算法动画演示可视化，用户可利用程序观看经典算法的执行过程，并可看到算法的代码。该平台功能包括对算法的动态可视化、用户对平台的反馈等功能。该平台是为在校学生与从业人员学习数据结构与算法搭建的平台，负责用户通过这个系统更直观、更深刻的学习数据结构与算法。"""
    area.insert("end", str1)
    area.grid(row=2, column=1)
    area.config(state="disable")
    weather_info = weather.main()
    add_tep = weather_info['city'] + weather_info['temp'] + '˚C' + weather_info['weather']
    now_date = weather_info['date'] + weather_info['week']
    detail = weather_info['index'][3]['detail']
    at_label = tkinter.Label(f3, text=add_tep, justify='right', fg="black", font=('', 25))
    at_label.grid(row=0, column=1)
    date_label = tkinter.Label(f1, text=now_date, justify='right', fg="black", font=('', 25))
    date_label.grid(row=0,column=1)
    detail_label = tkinter.Label(f3, text=detail, justify='left', fg="black", font=('', 15),wraplength=400)
    detail_label.grid(row=1,column=1)
    f4 = tkmacosx.SFrame(frame, width=900, height=100)
    f4.grid(row=3, column=0)
    liuyan = "如果你对本平台有任何疑问或者建议，欢迎点击留言按钮，你的建议是对我最大的帮助!!!"
    liuyan_label = tkinter.Label(f4, text=liuyan, justify='left', fg="black", font=('', 17),wraplength=700)
    liuyan_label.grid(row=0,column=0)
    lbutton = tkmacosx.Button(f4, text="点我留言", width=180, height=40, command=lambda :gui_liuyan.create_liuyan(usr))
    lbutton.grid(row=0, column=1)
    root.mainloop()


if __name__ == '__main__':
    create_index("apple")


